package com.zengdw.logRecord.resolve;

import com.zengdw.logRecord.config.LogRecord;
import com.zengdw.logRecord.domain.LogOperations;
import com.zengdw.logRecord.service.IOperatorService;
import org.springframework.util.StringUtils;

/**
 * @author zengd
 * @date 2021/9/18 16:41
 */
public class LogRecordOperationSource {
    private final IOperatorService operatorService;

    public LogRecordOperationSource(IOperatorService operatorService) {
        this.operatorService = operatorService;
    }

    public LogOperations computeLogRecordOperations(LogRecord annotation) {
        LogRecordContext.put("template", annotation.template());
        LogRecordContext.put("bizNo", annotation.bizNo());
        LogRecordContext.put("category", annotation.category());
        LogRecordContext.put("condition", annotation.condition());

        String operator = annotation.operator();
        if (!StringUtils.hasText(operator)) {
            operator = operatorService.getOperator();
        }
        LogRecordContext.put("operator", operator);

        return new LogOperations(annotation.template(), annotation.bizNo(), annotation.category(), annotation.condition(), operator);
    }
}
